package com.taxi.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxi.constant.AmapConfigConstants;
import com.taxi.dto.DicDistrict;
import com.taxi.mapper.DicDistrictMapper;
import com.taxi.result.Result;
import com.taxi.result.ResultCodeEnum;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: aniu
 * @Date: 2023/9/26
 * @ClassName: MapDicDistrictClient
 * @Description: Write class functionality and purpose here.
 * @Version: 1.0
 */
@Service
public class MapDicDistrictClient {
    @Value("${amap.key}")
    private String key;

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    public Result dicDistrict(String keywords){
        //拼装请求的url
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.DISTRICT_URL);
        url.append("?");
        url.append("keywords="+keywords);
        url.append("&");
        url.append("subdistrict=3");
        url.append("&");
        url.append("key="+key);

        ResponseEntity<String> forEntity = restTemplate.getForEntity(url.toString(), String.class);
        String dicDistrict = forEntity.getBody();
        return parseDistrictEntity(dicDistrict);
    }

    //解析Json
    public Result parseDistrictEntity(String dicDistrictString){

        JSONObject dicDistrictJsonObject = JSONObject.fromObject(dicDistrictString);
        int status = dicDistrictJsonObject.getInt(AmapConfigConstants.STATUS);
        if(status==0){
            return Result.fail(ResultCodeEnum.MAP_DISTRICT_ERROR);
        }
        JSONArray countryJsonArray = dicDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);

        for (int country=0;country<countryJsonArray.size();country++){
            JSONObject countryJsonObject = countryJsonArray.getJSONObject(country);
            String countryAddressCode = countryJsonObject.getString(AmapConfigConstants.ADCODE);
            String countryAddressName = countryJsonObject.getString(AmapConfigConstants.NAME);
            String countryParentAddressCode = "0";
            String countryLevel = countryJsonObject.getString(AmapConfigConstants.LEVEL);

            insertDicDistrict(countryAddressCode,countryAddressName,countryLevel,countryParentAddressCode);

            JSONArray proviceJsonArray = countryJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            for (int p = 0;p< proviceJsonArray.size();p++){
                JSONObject proviceJsonObject = proviceJsonArray.getJSONObject(p);
                String proviceAddressCode = proviceJsonObject.getString(AmapConfigConstants.ADCODE);
                String proviceAddressName = proviceJsonObject.getString(AmapConfigConstants.NAME);
                String proviceParentAddressCode = countryAddressCode;
                String proviceLevel = proviceJsonObject.getString(AmapConfigConstants.LEVEL);

                insertDicDistrict(proviceAddressCode,proviceAddressName,proviceLevel,proviceParentAddressCode);

                JSONArray cityArray = proviceJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                for (int city = 0;city< cityArray.size();city++){
                    JSONObject cityJsonObject = cityArray.getJSONObject(city);
                    String cityAddressCode = cityJsonObject.getString(AmapConfigConstants.ADCODE);
                    String cityAddressName = cityJsonObject.getString(AmapConfigConstants.NAME);
                    String cityParentAddressCode = proviceAddressCode;
                    String cityLevel = cityJsonObject.getString(AmapConfigConstants.LEVEL);

                    insertDicDistrict(cityAddressCode,cityAddressName,cityLevel,cityParentAddressCode);

                    JSONArray districtArray = cityJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    for (int d = 0;d< districtArray.size();d++){
                        JSONObject districtJsonObject = districtArray.getJSONObject(d);
                        String districtLevel = districtJsonObject.getString(AmapConfigConstants.LEVEL);
                        if(districtLevel.equals(AmapConfigConstants.STREET)){
                            continue;   // 街道就不存了
                        }
                        String districtAddressCode = districtJsonObject.getString(AmapConfigConstants.ADCODE);
                        String districtAddressName = districtJsonObject.getString(AmapConfigConstants.NAME);
                        String districtParentAddressCode = cityAddressCode;
                        insertDicDistrict(districtAddressCode,districtAddressName,districtLevel,districtParentAddressCode);
                    }
                }
            }

        }
        return Result.ok();
    }

    public void insertDicDistrict(String countryAddressCode,String countryAddressName,String countryLevel,String countryParentAddressCode){
        DicDistrict district = new DicDistrict();
        district.setAddressCode(countryAddressCode);
        district.setLevel(generateLevel(countryLevel));
        district.setParentAddressCode(countryParentAddressCode);
        district.setAddressName(countryAddressName);

        QueryWrapper<DicDistrict> wrapper = new QueryWrapper<>();
        wrapper.eq("address_code", countryAddressCode);
        wrapper.eq("address_name",countryAddressName);
        DicDistrict address_code = dicDistrictMapper.selectOne(wrapper);

        if(ObjectUtils.isEmpty(address_code)){
            dicDistrictMapper.insert(district);
        }
    }

    //定义国、省、市、区
    public int generateLevel(String level){
        int levelInt;
        switch (level){
            case "country":levelInt=0;break;
            case "province":levelInt=1;break;
            case "city":levelInt=2;break;
            case "district":levelInt=3;break;
            default:levelInt=4;
        }
        return levelInt;
    }
}
